<template>
  <!-- 新增部门的弹层 -->
  <el-dialog :title="showTitle" :visible="dialogVisible" @close="handleClose">
    <!-- 表单组件  el-form   label-width设置label的宽度   -->
    <!-- 匿名插槽 -->
    <el-form ref="deptForm" label-width="120px" :model="formData" :rules="rules">
      <el-form-item label="部门名称" prop="name">
        <el-input v-model="formData.name" style="width:80%" placeholder="1-50个字符" />
      </el-form-item>
      <el-form-item label="部门编码" prop="code">
        <el-input v-model="formData.code" style="width:80%" placeholder="1-50个字符" />
      </el-form-item>
      <el-form-item label="部门负责人" prop="manager">
        <el-select v-model="formData.manager" style="width:80%" placeholder="请选择" @focus="handleFocus">
          <el-option v-for="item in peoples" :key="item.id" :label="item.username" :value="item.username" />
        </el-select>
      </el-form-item>
      <el-form-item label="部门介绍" prop="introduce">
        <el-input v-model="formData.introduce" style="width:80%" placeholder="1-300个字符" type="textarea" :rows="3" />
      </el-form-item>
    </el-form>
    <!-- el-dialog有专门放置底部操作栏的 插槽  具名插槽 -->
    <el-row slot="footer" type="flex" justify="center">
      <!-- 列被分为24 -->
      <el-col :span="6">
        <el-button type="primary" size="small" @click="handleSubmit">确定</el-button>
        <el-button size="small" @click="handleClose">取消</el-button>
      </el-col>
    </el-row>
  </el-dialog>
</template>

<script>
import { getDepartmentsApi,
  getEmployeeSimpleApi,
  addDepartmentsApi,
  updateDepartmentsApi
} from '@/api/departments'
export default {
  name: 'AddDepart',
  components: {},
  props: {
    dialogVisible: {
      type: Boolean,
      default: false
    },
    crrentNode: {
      type: Object,
      default: () => ({})
    }
  },
  data() {
    const checkCodeRepeat = async(rules, value, callback) => {
      const { depts } = await getDepartmentsApi()
      //   console.log(depts)
      let flag = false
      if (this.formData.id) {
        console.log('编辑校验', this.currentNode)
        console.log(this.formData.id)
        flag = depts.some(item => item.id !== this.formData.id && item.code === value)
      } else {
        flag = depts.some(item => item.code === value)
      }
      flag ? callback(new Error(`部门编码${value}已存在，请重新输入`)) : callback()
    }
    const checkNameRepeat = async(rules, value, callback) => {
      const { depts } = await getDepartmentsApi()
      // console.log(depts)
      // console.log('名字校验crrentNode.id:', this.crrentNode.id)
      // console.log('名字校验formData.id:', this.formData.id)
      // console.log('名字校验formData.pid:', this.formData.pid)
      let flag = false
      if (this.formData.id) {
        const list = depts.filter(item => item.pid === this.formData.pid && item.id !== this.formData.id)
        // console.log('校验名字list:', list)
        flag = list.some(item => item.name === value)
      } else {
        const list = depts.filter(item => item.pid === this.formData.id)
        // console.log('list:', list)
        flag = list.some(item => item.name === value)
      }
      flag ? callback(new Error(`部门名称${value}已存在，请重新输入`)) : callback()
      // console.log(flag)
    }
    return {
      formData: {
        name: '', // 部门名称
        code: '', // 部门编码
        manager: '', // 部门管理者
        introduce: '' // 部门介绍
      },
      rules: {
        name: [
          { required: true, message: '部门名称必填', trigger: 'blur' },
          { min: 1, max: 50, message: ' 部门名称1-50个字符', trigger: 'blur' },
          {
            trigger: 'blur',
            validator: checkNameRepeat // 自定义函数的形式校验
          }
        ],
        code: [
          { required: true, message: '部门编码必填', trigger: 'blur' },
          { min: 1, max: 50, message: '部门编码1-50个字符', trigger: 'blur' },
          {
            trigger: 'blur',
            validator: checkCodeRepeat // 自定义函数的形式校验
          }
        ],
        manager: [
          { required: true, message: '部门负责人必填', trigger: 'blur' }
        ],
        introduce: [
          { required: true, message: '部门介绍必填', trigger: 'blur' },
          { min: 1, max: 300, message: '部门介绍1-300个字符', trigger: 'blur' }
        ]
      },
      peoples: []
    }
  },
  computed: {
    showTitle() {
      return this.formData.id ? '编辑部门' : '新增部门'
    }
  },
  watch: {},
  created() {},
  methods: {
    handleClose() {
      this.$emit('update:dialogVisible', false)
      this.$refs.deptForm.resetFields()
      this.formData = {
        name: '', // 部门名称
        code: '', // 部门编码
        manager: '', // 部门管理者
        introduce: '' // 部门介绍
      }
    },
    async handleFocus() {
      const res = await getEmployeeSimpleApi()
      // console.log(res)
      this.peoples = res
    },
    async handleSubmit() {
      try {
        const res = await this.$refs.deptForm.validate()
        console.log(res)
        this.formData.id ? await updateDepartmentsApi(this.formData) : await addDepartmentsApi({ ...this.formData, pid: this.crrentNode.id })
        this.$message.success(`${this.formData.id ? '新增' : '编辑'}部门成功`)
        this.handleClose()
        this.$parent.getDepartmentsFn()
      } catch (err) {
        console.log(err)
      }
    }
  }
}
</script>

<style scoped>

</style>
